# -*- coding: utf-8 -*-
"""
@File    : justdoo_warn
@Author  : Luyuan
@Date    : 2022-11-24 11:33:05
@Description: 警告信息
"""

import logging
from odoo import api, fields, models
from odoo.exceptions import UserError, ValidationError

_logger = logging.getLogger(__name__)


class JustdooWarning(models.TransientModel):
    _name = 'justdoo.warning'
    _description = '警告信息'

    res_model = fields.Char('资源模型')
    res_action = fields.Char('资源动作')
    warn_message = fields.Char('警告信息')
    res_ids = fields.Char('资源IDS', help='关联资源id多个资源id之间用,隔开')
    company_id = fields.Integer('公司ID', default=lambda self: self.env.company.id)
    company_name = fields.Char('公司', default=lambda self: self.env.company.name)

    def action_done(self):
        if not self.res_model:
            raise UserError('请显示设置好关联资源模型')
        if not self.res_ids:
            raise UserError('请显示设置好关联资源ID信息')
        if not self.res_action:
            raise UserError('请显示设置好关联资源动作')
        temp_res_ids = [int(res_id) for res_id in self.res_ids.split(',')]
        resource_list = self.env[self.res_model].sudo().browse(temp_res_ids)
        try:
            getattr(resource_list, self.res_action)()
        except Exception as ex:
            raise UserError(ex)
        return {'type': 'ir.actions.act_window_close'}



